﻿using QH.JobBack.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace QG.JobBack.DAL
{
    public class BaseRepository<T> : IRepository<T> where T : class

    {
        private DbContext db = DbContextFactory<qgpingEntities>.GetDbContext();
    /// <summary>
    /// 通用新增方法
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Add(T entity)
    {

        db.Set<T>().Add(entity);
        return db.SaveChanges() > 0;

    }

    /// <summary>
    /// 通用删除
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Delete(T entity)
    {
        db.Entry(entity).State = EntityState.Deleted;
        return db.SaveChanges() > 0;
    }

    /// <summary>
    /// 通用批量删除
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Delete(List<T> entitys)
    {
        foreach (var item in entitys)
        {
            var entity = db.Set<T>().Find(item);
            db.Set<T>().Remove(entity);
        }
        return db.SaveChanges() > 0;
    }
    /// <summary>
    /// 通用批量删除
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool Delete(params int[] ids)
    {
        foreach (var item in ids)
        {
            var entity = db.Set<T>().Find(item);
            db.Set<T>().Remove(entity);
        }
        return db.SaveChanges() > 0;
    }

        /// <summary>
        /// 根据ID来删除一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool DeleteById(int id)
        {
            var entity = db.Set<T>().Find(id);
            if (entity != null) db.Set<T>().Remove(entity);

            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 通用修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            db.Entry(entity).State = EntityState.Modified;
            return db.SaveChanges() > 0;
        }
    

    /// <summary>
    /// 通用查询集合
    /// </summary>
    public List<T> QueryList(Expression<Func<T, bool>> expression = null)
    {
        return db.Set<T>().Where(expression).ToList();
    }

    /// <summary>
    /// 查询返回对象
    /// </summary>
    /// <param name="expression"></param>
    /// <returns></returns>
    public T Query(Expression<Func<T, bool>> expression = null)
    {
        return db.Set<T>().Where(expression).SingleOrDefault();
    }

    /// <summary>
    /// 判断记录是否存在
    /// </summary>
    /// <param name="expression"></param>
    /// <returns></returns>
    public bool Exists(Expression<Func<T, bool>> expression = null)
    {
        return db.Set<T>().Where(expression).Any();
    }

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <returns></returns>
    public List<T> QueryPageList<S>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereExpression, Expression<Func<T, S>> orderExpression, bool isDsc = true)
    {
        if (!isDsc)
        {
            return db.Set<T>().Where(whereExpression)
                              .OrderByDescending(orderExpression)
                              .Skip((pageIndex - 1) * pageSize)
                              .Take(pageSize).ToList();
        }
        return db.Set<T>().Where(whereExpression)
                          .OrderBy(orderExpression)
                          .Skip((pageIndex - 1) * pageSize)
                          .Take(pageSize).ToList();
    }


        /// <summary>
        /// 分页查询2
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="whereLambda"></param>
        /// <param name="orderbyLambda"></param>
        /// <param name="total"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public List<T> QueryPageList<S>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderbyLambda, out int total, bool isAsc)
        {
            total = db.Set<T>().Where(whereLambda).Count();

            if (isAsc)
            {
                return
                db.Set<T>()
                  .Where(whereLambda)
                  .OrderBy(orderbyLambda)
                  .Skip(pageSize * (pageIndex - 1))
                  .Take(pageSize)
                  .ToList();
            }
            else
            {
                return
               db.Set<T>()
                 .Where(whereLambda)
                 .OrderByDescending(orderbyLambda)
                 .Skip(pageSize * (pageIndex - 1))
                 .Take(pageSize)
                 .ToList();
            }
        }
        /// <summary>
        /// 返回总数
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public int GetCount(Expression<Func<T, bool>> whereExpression, out int count)
    {
        return count = db.Set<T>().Where(whereExpression).Count();
    }

}
}
